package com.help.knowhelps.ui;

import com.help.knowhelps.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class UiUtils {
	/* Actionbar控件隐藏	 */
	public static int ACTIONBAR_BUTTON_DISAPPEAR = -2;
	/* Actionbar控件显示为默认图标	 */
	public static int ACTIONBAR_BUTTON_DEFAULT = -1;
	/**
	 * 初始化Actionbar,设置actionbar中各控件状态
	 * @param context 当前actionbar所在activity的上下文,必须是activity的上下文.
	 * @param title actionbar中title的文字内容
	 * @param DrawableRes_left actionbar左侧图片资源地址,传入ACTIONBAR_BUTTON_DISAPPEAR时,左侧按钮将被隐藏
	 * ACTIONBAR_BUTTON_DEFAULT时,显示按钮,默认为返回:<按钮.
	 * @param DrawableRes_right actionbar右侧图片资源地址,传入ACTIONBAR_BUTTON_DISAPPEAR时,右侧按钮将被隐藏
	 * ACTIONBAR_BUTTON_DEFAULT时,显示按钮,默认为下一步:>按钮.
	 */
	public static void InitActionBar(Context context,String title,int DrawableRes_left,int DrawableRes_right,OnClickListener listener)
	{
		
		
		if(DrawableRes_left != ACTIONBAR_BUTTON_DISAPPEAR)
		{
			ImageView leftbt = (ImageView) ((Activity) context).findViewById(R.id.actionbar_imageView_left);
			leftbt.setVisibility(View.VISIBLE);
			if(null != listener)
				leftbt.setOnClickListener(listener);
			if(DrawableRes_left != ACTIONBAR_BUTTON_DEFAULT)
				leftbt.setImageResource(DrawableRes_left);
		}
		if(DrawableRes_right != ACTIONBAR_BUTTON_DISAPPEAR)
		{
			ImageView rightbt = (ImageView)((Activity) context).findViewById(R.id.actionbar_imageView_right);
			rightbt.setVisibility(View.VISIBLE);
			if(null != listener)
				rightbt.setOnClickListener(listener);
			if(DrawableRes_left != ACTIONBAR_BUTTON_DEFAULT)
				rightbt.setImageResource(DrawableRes_right);
		}
		if(null != title)
			((TextView)((Activity)context).findViewById(R.id.actionbar_textView_title)).setText(title);
	}
	/**
	 * 创建自适应型Dialog.
	 * @param context 所在Activity上下文
	 * @param contentViewRes dialog布局文件
	 * @param listener 左右按钮点击监听.注:这里左右按钮id是写死的,不能自适应.
	 */
	@SuppressLint("NewApi")
	public static AlertDialog AdaptiveDialog(Context context,int contentViewRes,OnClickListener listener)
	{
		final AlertDialog dlg = new AlertDialog.Builder(context).create();
		dlg.setCanceledOnTouchOutside(true);// 点击其他区域dialog消失
		dlg.show();
		WindowManager.LayoutParams params = dlg.getWindow().getAttributes();
		int width;
		if (Integer.valueOf(android.os.Build.VERSION.SDK_INT) < 13) {
			Display display = ((Activity) context).getWindowManager().getDefaultDisplay();
			width = display.getWidth();
		} else {
			Display display = ((Activity) context).getWindowManager().getDefaultDisplay();
			android.graphics.Point size = new android.graphics.Point();
			display.getSize(size);
			width = size.x;
		}
		if (width == 480) {
			params.width = (int) (width * 0.75);
		}
		params.width = (int) (width * 0.9);
		dlg.getWindow().setAttributes(params);
		Window window = dlg.getWindow();
		// *** 主要就是在这里实现这种效果的.
		window.setContentView(contentViewRes);
		window.findViewById(R.id.info_dialog_textView_btcancel).setOnClickListener(listener);
		window.findViewById(R.id.info_dialog_textView_btsure).setOnClickListener(listener);
		window.findViewById(R.id.info_dialog_textView_btcancel).setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if(event.getAction() == KeyEvent.ACTION_UP)
					dlg.dismiss();
				return false;
			}
		});
		window.findViewById(R.id.info_dialog_textView_btsure).setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if(event.getAction() == KeyEvent.ACTION_UP)
					dlg.dismiss();
				return false;
			}
		});
		return dlg;
	}

}
